<?php
class UserInfoPresenter {
	private $view;
	private $userRepository;
	private $user;
	public function __construct($view, $userRepository, $userName) {
		$this->view = $view;
		$this->userRepository = $userRepository;
		$this->user = $this->userRepository->getUser ( $userName );
	}
	public function getUserName() {
		if ($this->user == NULL) {
			return NULL;
		}
		return htmlspecialchars ( $this->user->getUserName () );
	}
	public function getUser() {
		return $this->user;
	}
	public function getName() {
		if ($this->user == NULL) {
			return NULL;
		}
		
		return htmlspecialchars ( str_replace ( "\"", "", $this->user->getName () ) );
	}
	public function updateUser($userName, $name, $email, $currentPass, $newPass, $newPassConfirm) {
		if ($newPass !== $newPassConfirm) {
			$this->view->userUpdateError ( "Sweety, your passwords don't match. So do it again!" );
			return;
		}
		
		if (strlen ( $newPass ) < 6) {
			$this->view->userUpdateError ( "Can't read imbecile? More than 5 characters!" );
			
			return;
		}
		
		if (strlen ( $name ) == 0) {
			$this->view->userUpdateError ( "Strange name yo mofo. No way." );
			return;
		}
		
		if (! filter_var ( $email, FILTER_VALIDATE_EMAIL )) {
			$this->view->userUpdateError ( "It's not email you idiot!!! Calm down just kidding." );
			return;
		}
		$user = $this->userRepository->checkUser ( $userName, $currentPass );
		if ($user == NULL) {
			$this->view->userUpdateError ( "Forgot your password? Ha, screw you then!" );
			return;
		}
		
		$user->setPassword ( $newPass );
		$user->setName ( $name );
		$user->setEmail ( $email );
		if ($this->userRepository->updateUser ( $user )) {
			$this->view->userUpdateSucceed ();
		} else {
			$this->view->userUpdateError ( "OOPS!" );
		}
	}
	public function getEmail() {
		if ($this->user == NULL) {
			return NULL;
		}
		return htmlspecialchars ( $this->user->getEmail () );
	}
	public function getUserRole() {
		if ($this->user == NULL) {
			return NULL;
		}
		
		if ($this->user->getPrivileges () === User::USER_PRIVILEGES) {
			return "user";
		}
		
		if ($this->user->getPrivileges () === User::ADMIN_PRIVILEGES) {
			return "admin";
		}
	}
}

?>